const path = require('path')
const { app, BrowserWindow } = require('electron')
const WinState = require('electron-win-state').default
const createTray = require('./createTray')
require('./controller/getSource')
require('./controller/alert')
require('./controller/openWindow')
require('./controller/bulidMenu')

const creeateWindow = () => {
    // 自定义窗口状态
    const winState = new WinState({
        defaultWidth: 1000,
        defaultHeight: 800,
        electronStoreOptions: {
            name: 'window-state-main'
        }
    })
    const win = new BrowserWindow({
        ...winState.winOptions,
        minWidth: 460,
        minHeight: 440,
        show: false,
        icon: path.resolve(__dirname, './icon.ico'),
        webPreferences: {
            // preload预加载js
            preload: path.resolve(__dirname, './preload/index.js')
        }
    })
    win.on('ready-to-show', () => win.show())

    // win.loadURL('http://127.0.0.1:5173') //本地调试
    win.loadFile('./index.html')

    // win.webContents.openDevTools()

    winState.manage(win)

    createTray(app, win)
}

app.whenReady().then(() => {
    creeateWindow()

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) creeateWindow()
    })
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})